﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SIMS_GUI_Item
{
    public partial class StudentList : Form
    {
        public StudentList()
        {
            InitializeComponent();
        }

        SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-3CMC9MB\SQLEXPRESS;Initial Catalog=SIMS GUI Item DataBase;Integrated Security=True;");
        private void StudentList_Load(object sender, EventArgs e)
        {
            var sql = "select UserName,PassWord,RealName,IdentityCard,Sex,Birthday from UserInformation";
            SqlCommand comm = new SqlCommand(sql, conn);
            //创建数据适配器
            SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
            //创建数据集
            DataSet ds = new DataSet();
            //向数据集填充数据
            adp.Fill(ds);
            //是否允许自动创建列
            this.dgv_StudentList.AutoGenerateColumns = false;
            //不允许出现空行
            this.dgv_StudentList.AllowUserToAddRows = false;
            //整行选中
            this.dgv_StudentList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //是否允许多选
            this.dgv_StudentList.MultiSelect = false;
            this.dgv_StudentList.RowTemplate.Height = 50;
        }

        /// <summary>
        /// 搜索按钮
        /// </summary>
        SIMS_GUI_Item_DataBase db = new SIMS_GUI_Item_DataBase();
        private void btn_Search_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select a.UserName,a.PassWord,a.RealName,a.IdentityCard,b.GradeName,a.Sex,a.Birthday from UserInformation a
                        inner join Grade b on a.GradeId=b.GradeId where 1=1");//查询语句
            if (!string.IsNullOrWhiteSpace(this.txt_RealName.Text))
                sb.AppendFormat(" and a.RealName like '%{0}%'", this.txt_RealName.Text.Trim());
            SqlDataAdapter adp = new SqlDataAdapter(sb.ToString(), conn);
            DataSet ds = new DataSet();
            adp.Fill(ds);//填充数据
            this.dgv_StudentList.DataSource = ds.Tables[0];
        }
    }
}
